package com.mycompany.xxy_dome.view;
import com.mycompany.xxy_dome.controller.ImgController;
import com.mycompany.xxy_dome.util.SettingsPage;
import com.mycompany.xxy_dome.util.TextAreaFileReaderExample;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MenuGUI extends JFrame {

    private static int UI_WIDTH;
    private static int UI_HEIGHT;
    public MenuGUI(int x, int y) {
        UI_WIDTH = x;
        UI_HEIGHT = y;
        // 设置窗口标题
        setTitle("菜单示例");
        // 设置窗口大小
        setSize(450, 300);
        // 设置窗口关闭操作
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // 设置窗口居中显示
        setLocationRelativeTo(null);
        // 设置窗口大小不可变
        setResizable(false);
        scene();

    }

    public void scene() {
        // 创建菜单栏
        JMenuBar menuBar = new JMenuBar();
        Label label = new Label();

        // 创建菜单项
        JMenu featureMenu = new JMenu("功能");

        JMenuItem introdouce = new JMenuItem("介绍");
        JMenuItem settingsMenu = new JMenuItem("设置");
        JMenuItem  returnToMain = new JMenuItem("回到主页面");

        featureMenu.add(introdouce);
        featureMenu.add(settingsMenu);
        featureMenu.add(returnToMain);

        menuBar.add(featureMenu);
        // 将菜单栏添加到窗口
        setJMenuBar(menuBar);

        // 设置内容面板
        JPanel contentPanel = new JPanel();
        contentPanel.setLayout(new BorderLayout());
        contentPanel.add(new JLabel("欢迎使用本系统", SwingConstants.CENTER), BorderLayout.CENTER);
        setContentPane(contentPanel);

        // 设置窗口可见
        setVisible(true);



        introdouce.addActionListener(e1 -> {
            TextAreaFileReaderExample textAreaFileReaderExample = new TextAreaFileReaderExample();
            JTextArea textArea = TextAreaFileReaderExample.getJTextArea();
            label.setText(textArea.getText());
            contentPanel.removeAll();
            contentPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 0, 20)); // 添加外部边距
            contentPanel.add(textArea, BorderLayout.CENTER);
        });
        settingsMenu.addActionListener(e1 -> {
            contentPanel.removeAll();
            contentPanel.add(SettingsPage.getJPanel());
            setContentPane(contentPanel);
        });



        // 为JMenuItem添加ActionListener
        returnToMain.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 当菜单项被点击时调用的方法
                setVisible(false);
                ImgController imgController = new ImgController();
                imgController.Controller(UI_WIDTH, UI_HEIGHT);
            }
        });

    }



}

